<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加用户</title>
    <link rel="stylesheet" href="../../layui/css/layui.css">
</head>
<body>
<div style="padding-left: 5%;padding-top:5%">
    <form  class="layui-form" method="" action="" style="margin-top: 30px;">
        <input type="hidden" name="id">
        <!--第一个-->
        <div style="float: left;">
            <!--员工姓名-->
            <div class="layui-form-item">
                <label class="layui-form-label">用户账号：</label>
                <div class="layui-input-inline">
                    <input type="text" name="user_account" lay-verify="required|user_account" autocomplete="off" placeholder="请输入用户账号" class="layui-input">
                </div>
                <label class="layui-form-label">用户名称：</label>
                <div class="layui-input-inline">
                    <input type="text" name="user_name" lay-verify="required|user_name" autocomplete="off" placeholder="请输入用户姓名" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">用户电话：</label>
                <div class="layui-input-inline">
                    <input type="text" name="user_tel" lay-verify="required|user_tel" autocomplete="off" placeholder="请输入电话" class="layui-input">
                </div>
                <label class="layui-form-label">用户邮箱：</label>
                <div class="layui-input-inline">
                    <input type="text" name="user_email" lay-verify="required|user_email" autocomplete="off" placeholder="请输入邮箱" class="layui-input">
                </div>
            </div>

                <div class="layui-form-item">
                <label class="layui-form-label">用户角色：</label>
                <div class="layui-input-inline">
                    <div class="layui-input-inline">
                        <select name="role_id" class="layui-select" lay-verify="required" lay-filter="aihao">
                            <option value="">请选择</option>
                            <!--<option value="1">系统管理员</option>-->
                            <!--<option value="2">线路管理员</option>-->
                            <!--<option value="3">巡检员</option>-->
                            <!--<option value="4">消缺员</option>-->
                        </select>
                    </div>
                </div>
                <label class="layui-form-label">性别：</label>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="radio" name="sex" value="男" title="男" checked>
                        <input type="radio" name="sex" value="女" title="女">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">入职日期：</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="entry_time" id="entry_time">
                </div>
                <label class="layui-form-label">离职日期：</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="quit_time" id="quit_time">
                </div>
            </div>
            <!--手机号码、邮箱地址-->

            <!--提交按钮-->
            <div class="layui-form-item" style="padding-left: 130px;padding-top: 20px;">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" lay-submit="demo1" lay-filter="demo1" style="margin-right: 50px;">添加</button>
                    <button type="button" class="layui-btn layui-btn-normal" id="quit">取消</button>
                </div>
            </div>
        </div>
    </form>
    </div>
</body>
<script src="../../js/jquery-3.4.1.js"></script>
<script rel="script" src="../../layui/layui.js"></script>
<script rel="script" src="../../layui/layui.all.js"></script>
<script>
    layui.use(['form','upload','laydate'], function() {
            var upload = layui.upload
            , laydate = layui.laydate
            , layedit = layui.layedit
            , form = layui.form;

        //执行一个laydate实例
        laydate.render({
            elem: '#entry_time' //指定元素
        });
        laydate.render({
            elem: '#quit_time' //指定元素
        });
        $.ajax({
            url:"/queryRoleAll.do",
            type:"get",
            dataType:"json",
            success:function (data) {
                if (data.code == 0){
                $.each(data.data, function (index, value) {
                    $('select[name=role_id]').append(new Option(value.role_name,value.role_id));// 下拉菜单里添加元素
                });
                layui.form.render("select");//重新渲染 固定写法
            }
            }
        });

        var flag=true;
        $("input[name=user_account]").blur(function () {
            var userAccount=$("input[name=user_account]").val();
            $.ajax({
                url:"/judgeUserAcc.do",
                type:"post",
                data:{userAccount:userAccount},
                success:function (res) {
                    if (res.code == 500){
                        layer.msg(res.msg,{icon:5});
                        flag=false;
                    }else if (res.code == 502) {
                        layer.msg(res.msg,{icon:5});
                    }else if (res.code == 405){
                        layer.msg(res.msg,{icon:5});
                    }else {
                        flag=true;
                    }
                }

            })
        });
        //自定义验证规则
        form.verify({
            user_account:function(value){
                if (value.length < 6) {
                    return '用户名称至少得6个字符';
                }
                if (!flag){
                    return '用户账号不能重复';
                }
            },
            user_tel: function (value) {
                var reg = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9]|17[0|1|2|3|5|6|7|8|9])\d{8}$/;
                if (!reg.test(value)) {
                    return '手机格式不正确';
                }
            },
            user_name: function (value) {
                if (value.length < 2) {
                    return '用户名称至少得2个字符';
                }
            },
            user_email: function (value) {
                var reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
                if (!reg.test(value)) {
                    return '邮箱格式不符';
                }
            }
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });

        form.on('submit(demo1)',function (data) {
            $.ajax({
                url:"/addUser.do",
                type:"post",
                data:JSON.stringify(data.field),
                success:function (res) {
                    if (res.code == 200){
                        layer.msg(res.msg,{icon:1});
                        var second=1;
                        var interval = setInterval(function () {
                            second--;
                            if (second === -1) {
                                parent.layer.close(parent.layer.index);
                                clearInterval(interval);
                            }
                        }, 1000);
                    } else if (res.code == 500){
                        layer.msg(res.msg,{icon:5});
                    } else if (res.code == 502){
                        layer.msg(res.msg,{icon:5});
                    }else if (res.code == 405){
                        layer.msg(res.msg,{icon:5});
                    }
                }
            })
        })

    });

    $(function () {
        $("#quit").click(function () {
            parent.layer.close(parent.layer.index);
        })
    });
</script>
</html>